]> git.saurik.com Git - apple/libdispatch.git/blob - examples/Dispatch Samples/nWide.c
libdispatch-84.5.1.tar.gz
[apple/libdispatch.git] / examples / Dispatch Samples / nWide.c
1 /*
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_DTS_LICENSE_HEADER_START@
5 *
6 * IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
7 * ("Apple") in consideration of your agreement to the following terms, and your
8 * use, installation, modification or redistribution of this Apple software
9 * constitutes acceptance of these terms. If you do not agree with these terms,
10 * please do not use, install, modify or redistribute this Apple software.
11 *
12 * In consideration of your agreement to abide by the following terms, and
13 * subject to these terms, Apple grants you a personal, non-exclusive license,
14 * under Apple's copyrights in this original Apple software (the "Apple Software"),
15 * to use, reproduce, modify and redistribute the Apple Software, with or without
16 * modifications, in source and/or binary forms; provided that if you redistribute
17 * the Apple Software in its entirety and without modifications, you must retain
18 * this notice and the following text and disclaimers in all such redistributions
19 * of the Apple Software. Neither the name, trademarks, service marks or logos of
20 * Apple Computer, Inc. may be used to endorse or promote products derived from
21 * the Apple Software without specific prior written permission from Apple. Except
22 * as expressly stated in this notice, no other rights or licenses, express or
23 * implied, are granted by Apple herein, including but not limited to any patent
24 * rights that may be infringed by your derivative works or by other works in
25 * which the Apple Software may be incorporated.
26 *
27 * The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
28 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
29 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
31 * COMBINATION WITH YOUR PRODUCTS.
32 *
33 * IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
35 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
37 * DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
38 * CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
39 * APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * @APPLE_DTS_LICENSE_HEADER_END@
42 */
43
44 /*
45 * nWide.c
46 * Samples project
47 *
48 * Created by Mensch on 5/1/09.
49 * Copyright 2009 Apple, Inc. All rights reserved.
50 *
51 */
52
53 #include <stdio.h>
54 #include <unistd.h>
55 #include <stdlib.h>
56 #include <sys/errno.h>
57 #include <assert.h>
58 #include <dispatch/dispatch.h>
59 #include <mach/mach_time.h>
60 #import <libkern/OSAtomic.h>
61 #include <string.h>
62
63
64 /*
65 * Demonstrate using dispatch_semaphore to create a concurrent queue that
66 * allows only a fixed number of blocks to be in flight at any given time
67 */
68
69 int main (int argc, const char * argv[]) {
70 dispatch_group_t mg = dispatch_group_create();
71 dispatch_semaphore_t ds;
72 __block int numRunning = 0;
73 int qWidth = 5;
74 int numWorkBlocks = 100;
75
76 if (argc >= 2) {
77 qWidth = atoi(argv[1]); // use the command 1st line parameter as the queue width
78 if (qWidth==0) qWidth==1; // protect against bad values
79 }
80
81 if (argc >=3) {
82 numWorkBlocks = atoi(argv[2]); // use the 2nd command line parameter as the queue width
83 if (numWorkBlocks==0) numWorkBlocks==1; // protect against bad values
84 }
85
86 printf("Starting dispatch semaphore test to simulate a %d wide dispatch queue\n", qWidth );
87 ds = dispatch_semaphore_create(qWidth);
88
89 int i;
90 for (i=0; i<numWorkBlocks; i++) {
91 // synchronize the whole shebang every 25 work units...
92 if (i % 25 == 24) {
93 dispatch_group_async(mg,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
94 int x;
95 // wait for all pending work units to finish up...
96 for (int x=0; x<qWidth; x++) dispatch_semaphore_wait(ds, DISPATCH_TIME_FOREVER);
97 // do the thing that is critical here
98 printf("doing something critical...while %d work units are running \n",numRunning);
99 // and let work continue unimpeeded
100 for (int x=0; x<qWidth; x++) dispatch_semaphore_signal(ds);
101 });
102 } else {
103 // schedule the next block waiting when there are qWidth blocks running
104 dispatch_semaphore_wait(ds, DISPATCH_TIME_FOREVER);
105 dispatch_group_async(mg,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
106 OSAtomicIncrement32( &numRunning );
107 usleep(random() % 10000); // simulate some random amount of work
108 printf("Value of i is %d Number of blocks in flight %d\n",i, numRunning);
109 // tell the loop it's time to schedule the next block if there is one
110 OSAtomicDecrement32( &numRunning );
111 dispatch_semaphore_signal(ds);
112 });
113 }
114 }
115
116 dispatch_group_notify(mg, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
117 printf("And we are done!\n");
118 dispatch_release(mg);
119 dispatch_release(ds);
120 exit(0);
121 });
122
123 dispatch_main();
124 return 0;
125 }
126
127